package androidx.test.espresso.base;

import android.os.Build;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import androidx.test.espresso.Root;
import androidx.test.espresso.core.internal.deps.guava.base.Preconditions;
import androidx.test.espresso.core.internal.deps.guava.collect.Lists;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public final class RootsOracle implements ActiveRootLister {
    private static final String GET_DEFAULT_IMPL = "getDefault";
    private static final String GET_GLOBAL_INSTANCE = "getInstance";
    private static final String TAG = "RootsOracle";
    private static final String VIEWS_FIELD = "mViews";
    private static final String WINDOW_MANAGER_GLOBAL_CLAZZ = "android.view.WindowManagerGlobal";
    private static final String WINDOW_MANAGER_IMPL_CLAZZ = "android.view.WindowManagerImpl";
    private static final String WINDOW_PARAMS_FIELD = "mParams";
    private boolean initialized;
    private final Looper mainLooper;
    private Field paramsField;
    private Field viewsField;
    private Object windowManagerObj;

    /* JADX INFO: Access modifiers changed from: package-private */
    public RootsOracle(Looper mainLooper) {
        this.mainLooper = mainLooper;
    }

    @Override // androidx.test.espresso.base.ActiveRootLister
    public List<Root> listActiveRoots() {
        List list;
        List list2;
        Preconditions.checkState(this.mainLooper.equals(Looper.myLooper()), "must be called on main thread.");
        if (!this.initialized) {
            initialize();
        }
        if (this.windowManagerObj == null) {
            Log.w(TAG, "No reflective access to windowmanager object.");
            return Lists.newArrayList();
        } else if (this.viewsField == null) {
            Log.w(TAG, "No reflective access to mViews");
            return Lists.newArrayList();
        } else if (this.paramsField == null) {
            Log.w(TAG, "No reflective access to mParams");
            return Lists.newArrayList();
        } else {
            try {
                if (Build.VERSION.SDK_INT < 19) {
                    list = Arrays.asList((View[]) this.viewsField.get(this.windowManagerObj));
                    list2 = Arrays.asList((WindowManager.LayoutParams[]) this.paramsField.get(this.windowManagerObj));
                } else {
                    list = (List) this.viewsField.get(this.windowManagerObj);
                    list2 = (List) this.paramsField.get(this.windowManagerObj);
                }
                ArrayList newArrayList = Lists.newArrayList();
                for (int size = list.size() - 1; size > -1; size--) {
                    newArrayList.add(new Root.Builder().withDecorView((View) list.get(size)).withWindowLayoutParams((WindowManager.LayoutParams) list2.get(size)).build());
                }
                return newArrayList;
            } catch (IllegalAccessException e) {
                Log.w(TAG, String.format("Reflective access to %s or %s on %s failed.", this.viewsField, this.paramsField, this.windowManagerObj), e);
                return Lists.newArrayList();
            } catch (RuntimeException e2) {
                Log.w(TAG, String.format("Reflective access to %s or %s on %s failed.", this.viewsField, this.paramsField, this.windowManagerObj), e2);
                return Lists.newArrayList();
            }
        }
    }

    private void initialize() {
        this.initialized = true;
        String str = Build.VERSION.SDK_INT > 16 ? WINDOW_MANAGER_GLOBAL_CLAZZ : WINDOW_MANAGER_IMPL_CLAZZ;
        String str2 = Build.VERSION.SDK_INT > 16 ? GET_GLOBAL_INSTANCE : GET_DEFAULT_IMPL;
        try {
            Class<?> cls = Class.forName(str);
            this.windowManagerObj = cls.getMethod(str2, new Class[0]).invoke(null, new Object[0]);
            Field declaredField = cls.getDeclaredField(VIEWS_FIELD);
            this.viewsField = declaredField;
            declaredField.setAccessible(true);
            Field declaredField2 = cls.getDeclaredField(WINDOW_PARAMS_FIELD);
            this.paramsField = declaredField2;
            declaredField2.setAccessible(true);
        } catch (ClassNotFoundException e) {
            Log.e(TAG, String.format("could not find class: %s", str), e);
        } catch (IllegalAccessException e2) {
            Log.e(TAG, String.format("reflective setup failed using obj: %s method: %s field: %s", str, str2, VIEWS_FIELD), e2);
        } catch (NoSuchFieldException e3) {
            Log.e(TAG, String.format("could not find field: %s or %s on %s", WINDOW_PARAMS_FIELD, VIEWS_FIELD, str), e3);
        } catch (NoSuchMethodException e4) {
            Log.e(TAG, String.format("could not find method: %s on %s", str2, str), e4);
        } catch (RuntimeException e5) {
            Log.e(TAG, String.format("reflective setup failed using obj: %s method: %s field: %s", str, str2, VIEWS_FIELD), e5);
        } catch (InvocationTargetException e6) {
            Log.e(TAG, String.format("could not invoke: %s on %s", str2, str), e6.getCause());
        }
    }
}
